#! /bin/bash
#
# bootstrap: Utility to easy autoconf/automake toolchain setup
#            Forcing specific tool versions.

dir=`echo "$0" | sed 's,[^/]*$,,'`
test "x${dir}" = "x" && dir='.'

if (test "x`cd "${dir}" 2>/dev/null && pwd`" != "x`pwd`"); then
    echo "This script must be executed directly from the source directory."
    exit 1
fi


###############################################################################
function check_autotool_prerequisites ()
{
    # prerequisite versions
    automake_major=1
    automake_minor=12

    autoconf_major=2
    autoconf_minor=61

    build_autotools=0


    autoreconf=`which autoreconf 2>/dev/null`
    if (test "x$autoreconf" != "x"); then
	if (test -x $autoreconf); then
	    autoconf_version=`$autoreconf --version | grep " $autoconf_major\." | cut -d"." -f2`
	    if (test $autoconf_version -lt $autoconf_minor); then
		echo "Autoconf Version $autoconf_major.$autoconf_version too old, building a new one..."
		build_autotools=1
	    else
		version_id=`$autoreconf --version 2>&1 | grep "(GNU Autoconf)"`
		echo "Using autoconf ($version_id)"
	    fi
	else
	    build_autotools=1
	fi
    else
	build_autotools=1
    fi


    libtool=`which glibtool 2>/dev/null`
    if (test "x$libtool" = "x"); then
	libtool=`which libtool 2>/dev/null`
    fi
    if (test "x$libtool" != "x"); then
	if (test -x $libtool); then
	    libtool_version=`$libtool --version 2>&1 | grep "(GNU libtool)"`

	    case "$libtool_version" in
		*2.4* | *2.2*)
		    echo "Using libtool ($libtool_version)"
		    ;;
		*)
	            echo "Indaequate Libtool ($libtool_version), building a new one..."
		    build_autotools=1
	    esac
	else
	    build_autotools=1
	fi
    else
	build_autotools=1
    fi


    automake=`which automake 2>/dev/null`
    if (test "x$automake" != "x"); then
	if (test -x $automake); then
	    automake_version=`$automake --version | grep " $automake_major\." | cut -d"." -f2`
	    if (test $automake_version -lt $automake_minor); then
		echo "Automake Version $automake_major.$automake_version too old, building a new one..."
		build_autotools=1
	    else
		version_id=`$automake --version 2>&1 | grep "(GNU automake)"`
		echo "Using automake ($version_id)"
	    fi
	else
	    build_autotools=1
	fi
    else
	build_autotools=1
    fi


    return $build_autotools
}
###############################################################################



check_autotool_prerequisites
build_autotools=$?

###############################################################################
for arg in $@; do
    if (test "x$arg" = "x--build-autotools"); then
	echo "building autotools at user request"
	build_autotools=1
    fi
done


###############################################################################
if (test "x$build_autotools" = "x1"); then
    # when there is no autoreconf try to actually build one.
    echo "Inadequate autotools in your path, I'll build new ones..."

    top_srcdir=`pwd`
    autotools_distdir=$top_srcdir/externals/autotools


    if (test -d $autotools_distdir); then
    # set the path to use the new tools as they are installed.
    # for example, the new autoconf is needed to build automake.
	PATH=$autotools_distdir/bin:$PATH
	for tool in autoconf-2.69 automake-1.12.5 libtool-2.4.2 ; do
	    cd $autotools_distdir
	    echo "  building $tool in $autotools_distdir"
	    tar zxf $tool.tar.gz
	    cd $tool && ./configure --prefix=$autotools_distdir >/dev/null && make install >/dev/null
	    cd .. && rm -rf $tool
	done

	cd $top_srcdir

	if (test -x $autotools_distdir/bin/autoreconf -a -x $autotools_distdir/bin/automake -a -x $autotools_distdir/bin/libtool); then
	    echo " "
	    echo " --> successfully installed autoreconf in $autotools_distdir/bin - please update your PATH and try again!"
	    echo " "
	    PATH=$autotools_distdir/bin:$PATH ./$0
	    exit 0
	fi
    fi
fi
###############################################################################



autoreconf=`which autoreconf 2>/dev/null`
# prefer autoreconf when it is available
if (test "x$autoreconf" != "x"); then
    if (test -x $autoreconf); then
	$autoreconf -iv --force
	exit 0
    fi
fi
